/*
 * $Id: JsonNavigationConnector.java 56 2009-05-11 10:11:46Z donbeave $
 */

package labour.dbmanager.client.connector.json;

import java.util.List;

import labour.concept.client.connector.SimpleConnector;
import labour.dbmanager.client.connector.NavigationConnector;
import labour.dbmanager.client.data.DataBaseItemModelType;
import labour.dbmanager.client.data.NavigationLoader;
import labour.dbmanager.client.data.json.JsonNavigationTreeReader;
import labour.dbmanager.client.model.Column;
import labour.dbmanager.client.model.DataBaseItem;
import labour.dbmanager.client.model.Schema;
import labour.dbmanager.client.model.Table;

import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.HttpProxy;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class JsonNavigationConnector extends SimpleConnector implements NavigationConnector {

  private DataBaseItemModelType modelType;

  public JsonNavigationConnector(String URL) {
    super(URL);

    modelType = new DataBaseItemModelType();
  }

  public void getSchemas(AsyncCallback<List<Schema>> callback) {

  }

  public void getColumns(Table table, AsyncCallback<List<Column>> callback) {

  }

  public void getTables(Schema schema, AsyncCallback<List<Table>> callback) {

  }

  @SuppressWarnings("unchecked")
  public HttpProxy getProxy() {
    return (HttpProxy) super.getProxy();
  }

  @SuppressWarnings("unchecked")
  public JsonNavigationTreeReader getReader() {
    return (JsonNavigationTreeReader) super.getReader();
  }

  public NavigationLoader getLoader() {
    return (NavigationLoader) super.getLoader();
  }

  public TreeStore<DataBaseItem> getStore() {
    return (TreeStore<DataBaseItem>) super.getStore();
  }


  @Override
  protected void createProxy() {
    RequestBuilder request = new RequestBuilder(RequestBuilder.GET, getUrl());

    HttpProxy<List<DataBaseItem>> proxy = new HttpProxy<List<DataBaseItem>>(request) {
      @Override
      public void load(final DataReader<List<DataBaseItem>> reader, final Object loadConfig, final AsyncCallback<List<DataBaseItem>> callback) {
        if (loadConfig == null) {
          initUrl = getUrl() + "/schemas/";
        }
        else if (loadConfig instanceof Schema) {
          initUrl = getUrl() + "/database/" + URL.encodeComponent(((Schema) loadConfig).getName()) + "/";
        }
        else if (loadConfig instanceof Table) {
          Table table = (Table) loadConfig;
          initUrl = getUrl() + "/database/" + URL.encodeComponent((String) table.get(Table.SCHEMA_NAME)) + "/" + URL.encodeComponent(table.getName()) + "/";
        }

        super.load(reader, loadConfig, callback);
      }
    };

    this.proxy = proxy;
  }

  @Override
  protected void createReader() {
    reader = new JsonNavigationTreeReader(modelType);
  }

  @Override
  protected void createLoader() {
    loader = new NavigationLoader(this);
  }

  @Override
  protected void createStore() {
    store = new TreeStore(getLoader());
  }

}